package com.qqin.controller;


import com.qqin.pojo.Comment;
import com.qqin.pojo.PageBean;
import com.qqin.pojo.Result;
import com.qqin.pojo.dto.CommUser;
import com.qqin.service.CommentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@CrossOrigin
@RestController//直接返回 JSON 或 XML 等格式的数据
@RequestMapping("/comments")
public class CommentController {
    @Autowired
    private CommentService commentService;

    //根据商品ID查询评论
    @GetMapping("/{goodsId}")
    public Result selectByGoods(@PathVariable Integer goodsId) {
        log.info("根据商品查询评论");
        List<CommUser> comments = commentService.selectByGoods(goodsId);

        return Result.success(comments);
    }

    //分页查询所有评论
    @GetMapping("/page")
    public Result page(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("分页查询所有评论");
        PageBean pageBean = commentService.page(page, pageSize);

        return Result.success(pageBean);
    }

    //添加评论
    @PostMapping("/add")
    public Result add(@RequestBody Comment comment) {
        log.info("发表评论");
        commentService.add(comment);

        return Result.success();
    }

}
